// eslint-disable-next-line no-restricted-imports
import { cloneDeep, flatten, isNumber } from 'lodash'
import { filterConditon, conditionE } from './filterData'
import { Message } from 'element-ui'
interface Option {
  businessId: string
  fieldType: string
  filterType: number | '',
  logicalType: number, // 逻辑关系，1，2（且和或）
  filterValue?: null | number[] | string | string[]
  minFilterValue?: number | null | string
  maxFilterValue?: number | null | string
  sourceFilter?: Option // 关联字段有
}
const rangeValue = [conditionE.inScope, conditionE.notInScope]
const disabledValue = [conditionE.isNull, conditionE.isNotNull]
const option: Option = { businessId: '', fieldType: '', filterType: '', logicalType: 1 }
// 多层关联字段嵌套递归
export const newRecursion = (sourceField) => {
  if (sourceField && sourceField.fieldType === 'associationField') {
    return newRecursion(sourceField.sourceField);
  } else {
    return sourceField;
  }
}
// 获取筛选项
export function getOption (conditon?: any) {
  const opt: any = cloneDeep(option)
  if (!conditon) {
    return opt
  }
  const item = newRecursion(conditon)
  opt.businessId = item.businessId
  opt.fieldType = item.fieldType
  if (conditon.subFieldType) {
    opt.filterType = filterConditon[item.subFieldType + 'Filter'][0].value
  } else {
    opt.filterType = filterConditon[item.fieldType + 'Filter'][0].value
  }
  if (conditon.trigger === 'webhook') {
    opt.trigger = 'webhook'
  }
  if (rangeValue.includes(opt.filterType)) {
    opt.maxFilterValue = item.fieldType === 'number' ? null : ''
    opt.minFilterValue = item.fieldType === 'number' ? null : ''
  } else {
    opt.filterValue = item.fieldType === 'number' ? null : ''
  }
  if (conditon.fieldType === 'associationField') {
    return { businessId: conditon.businessId, fieldType: conditon.fieldType, sourceFilter: opt }
  } else {
    return opt
  }
}
// 该方法验证筛选是否合法
export function isValide (filters, isMessage = true) {
  return !flatten<any>(filters || []).some((item) => {
    if (!item.businessId) {
      // 筛选项不能为空
      if (isMessage) {
        Message({
          message: '筛选项不能为空',
          type: 'error',
          duration: 3 * 1000
        })
      }
      return true
    } else {
      // associationField是关联字段
      const conditon = item.fieldType === 'associationField' ? item.sourceFilter : item
      if (rangeValue.includes(conditon.filterType)) { // 在范围内 不在范围内
        if (hasValue(conditon.minFilterValue) || hasValue(conditon.maxFilterValue)) {
          return false
        }
      } else if (disabledValue.includes(conditon.filterType)) { // 为空不为空
        return false
      } else if (hasValue(conditon.filterValue)) {
        return false
      }
      if (isMessage) {
        Message({
          message: '筛选内容不能为空',
          type: 'error',
          duration: 3 * 1000
        })
      }
      return true
    }
  })
}
// 判定是否有值
function hasValue (v) {
  if (v instanceof Array) {
    return v.length
  } else if (isNumber(v)) {
    return true
  } else {
    return v
  }
}
